import Kingfisher
import UIKit

enum WhichCollection {
  case starred
  case potential
}

class CustomerCell: UICollectionViewCell {

  /**
   * 客户头像
   */
  private lazy var avatarImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    imageView.layer.cornerRadius = 25
    imageView.clipsToBounds = true
    imageView.layer.borderColor = UIColor.cyan.cgColor
    imageView.layer.borderWidth = 0.5
    return imageView
  }()

  /**
   * 客户名称标签
   */
  private lazy var nameLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textAlignment = .center
    label.font = .systemFont(ofSize: 14)
    return label
  }()

  var onStatusButtonTapped: (() -> Void)?
  /**
   * 状态按钮（+ 或 ✓）
   */
  private lazy var statusButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = .systemBlue
    button.layer.cornerRadius = 10
    button.tintColor = .white
    button.layer.borderColor = UIColor.white.cgColor
    button.layer.borderWidth = 2.0
    button.titleEdgeInsets = UIEdgeInsets(top: -1, left: 0, bottom: 1, right: 0)
    button.addTarget(self, action: #selector(statusAction), for: .touchUpInside)
    return button
  }()

  override init(frame: CGRect) {
    super.init(frame: frame)
    setupUI()
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  /**
   * 配置单元格
   * @param customer 客户数据
   * @param isFollowed 是否已关注
   */
  func configure(with customer: Customer, which: WhichCollection) {
    if let urlString = customer.headImageUrl, let url = URL(string: urlString) {
      avatarImageView.kf.setImage(with: url, placeholder: UIImage(named: "default_avatar"))
    } else {
      avatarImageView.image = UIImage(named: "default_avatar")
    }
    nameLabel.text = customer.nickName

    if which == .starred {
      statusButton.setTitle("-", for: .normal)
      statusButton.backgroundColor = UIColor(hex: "#FE2E2E")
    } else {
      // 0-已关注 1-未关注
      if customer.followVisitorStatus == 0 && customer.followType == 0 {
        statusButton.setTitle("✓", for: .normal)
        statusButton.backgroundColor = UIColor(hex: "#CCCCCC")
      } else {
        statusButton.setTitle("+", for: .normal)
        statusButton.backgroundColor = UIColor(hex: "#007AFF")
      }
    }

    layoutIfNeeded()  // 刷新布局
    avatarImageView.layer.cornerRadius = avatarImageView.bounds.width / 2
    avatarImageView.layer.masksToBounds = true
  }

  let imageSizeMultiplier: CGFloat = 0.9
  func setupUI() {
    contentView.addSubview(avatarImageView)
    contentView.addSubview(nameLabel)
    contentView.addSubview(statusButton)

    NSLayoutConstraint.activate([
      // 头像约束
      avatarImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
      avatarImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
      avatarImageView.widthAnchor.constraint(
        equalTo: contentView.widthAnchor, multiplier: imageSizeMultiplier),
      avatarImageView.heightAnchor.constraint(equalTo: avatarImageView.widthAnchor),  // 保持正方形！

      // 名称标签约束
      nameLabel.topAnchor.constraint(equalTo: avatarImageView.bottomAnchor, constant: 5),
      nameLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
      nameLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
      nameLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),

      // 状态按钮约束
      statusButton.topAnchor.constraint(equalTo: avatarImageView.topAnchor, constant: -2),
      statusButton.trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: 2),
      statusButton.widthAnchor.constraint(equalToConstant: 20),
      statusButton.heightAnchor.constraint(equalToConstant: 20),
    ])
  }

  @objc private func statusAction() {
    onStatusButtonTapped?()
  }
}
